package com.gmall.search.service;

import com.gmall.common.constants.MqConst;
import com.gmall.common.result.Result;
import com.gmall.model.product.BaseTrademark;
import com.gmall.model.product.SkuInfo;
import com.gmall.model.product.dto.Category123IdNameDTO;
import com.gmall.model.search.Goods;
import com.gmall.product.feign.ProductApiFeignClient;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SkuSaleCancelService {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    ProductApiFeignClient productApiFeignClient;
    @RabbitListener(queues = MqConst.QUEUE_GOODS_UPPER)
    @RabbitHandler
    public void receiveOnSaleMessage(Channel channal, Message message, String skuId){
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {

            String skuId1 = new String(message.getBody());
            System.out.println(skuId +"---------" + skuId1);

            Result<SkuInfo> skuInfo_result = productApiFeignClient.getSkuInfoById(Long.parseLong(skuId));
            if(skuInfo_result.isOk()){
                SkuInfo skuInfo = skuInfo_result.getData();
                Goods goods = new Goods();
                goods.setId(skuInfo.getId());
                goods.setCategory3Id(skuInfo.getCategory3Id());

                Result<Category123IdNameDTO> category123IdNameDTOResult
                        = productApiFeignClient.getCategory123IdNameDTO(skuInfo.getCategory3Id());
                if(category123IdNameDTOResult.isOk()){
                    Category123IdNameDTO category123IdNameDTO = category123IdNameDTOResult.getData();

                    goods.setCategory1Id(category123IdNameDTO.getCategory1Id());//2, 30, 192
                    goods.setCategory2Id(category123IdNameDTO.getCategory2Id());

                    goods.setCategory1Name(category123IdNameDTO.getCategory1Name());
                    goods.setCategory2Name(category123IdNameDTO.getCategory2Name());
                    goods.setCategory3Name(category123IdNameDTO.getCategory3Name());
                }

                goods.setTmId(skuInfo.getTmId());

                Result<BaseTrademark> trademarkResult
                        = productApiFeignClient.getTrademarkById(skuInfo.getTmId());

                if(trademarkResult.isOk()){
                    BaseTrademark trademark = trademarkResult.getData();
                    goods.setTmName(trademark.getTmName());
                    goods.setTmLogoUrl(trademark.getLogoUrl());
                }
                goods.setPrice(skuInfo.getPrice().doubleValue());
                goods.setCreateTime(skuInfo.getCreateTime());
                goods.setTitle(skuInfo.getSkuName());
                goods.setDefaultImg(skuInfo.getSkuDefaultImg());

                elasticsearchRestTemplate.save(goods);//新增或者更新
            }

            channal.basicAck(deliveryTag,false);
        }catch (Exception e){
            try {
                channal.basicNack(deliveryTag,false,true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }


    @RabbitListener(queues = MqConst.QUEUE_GOODS_LOWER)
    @RabbitHandler
    public void receiveOnCancelSaleMessage(Channel channal, Message message, String skuId){
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try{
            elasticsearchRestTemplate.delete(skuId,Goods.class);
            channal.basicAck(deliveryTag,false);
        }catch (Exception e){
            try {
                channal.basicNack(deliveryTag,false,true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
